/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.business.security.boundary;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import meteocal.business.security.entity.Group;
import meteocal.business.security.entity.User;

/**
 *
 * @author luana
 */

@Stateless
public class UserManager {
    
    @PersistenceContext(unitName = "meteocalPU")
            
    EntityManager em;
    
    private String username;
    private String city;
    private String name;
    
    /**
     * This interface represents the abstract notion of a principal, which
     * can be used to represent any entity, such as an individual, a
     * corporation, and a login id.
     */
    @Inject
    Principal principal;
    
    public void save (User user){
       user.setGroupname(Group.USERS);
       em.persist(user);
    }
    
    public User find(String username){
        return em.find(User.class, username);
    }
    
    public void delete (User user){
        em.remove(getLoggedUser());
    }
    
    public User getLoggedUser (){
        return em.find(User.class, principal.getName());      
    }
    
    public void updateUser(User user){
        em.merge(user);
        em.flush();
    }
   
}